package com.lx.generator.monitor;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author lX
 * @version JDK 8
 * @className TpsCounter (此处以class为例)
 * @date 2024/11/10
 * @description TPS计算器
 */
public class TpsCounter {

    private AtomicLong requestCount = new AtomicLong(0);
    private long lastResetTime = System.currentTimeMillis();

    public void increment() {
        requestCount.incrementAndGet();
    }


    public double getTps() {
        long currentTime = System.currentTimeMillis();
        double elapsedSeconds = (currentTime - lastResetTime) / 1000.0;
        double tps = requestCount.get() / elapsedSeconds;
        reset();
        return tps;
    }

    private void reset() {
        requestCount.set(0);
        lastResetTime = System.currentTimeMillis();
    }
}
